package com.yuanheng100.ems4j.common.security;

/**
 * 数据加密辅助类(默认编码UTF-8)
 *
 * @author ShenHuaJie
 * @since 2011-12-31
 */
public final class SecurityUtil
{
    private SecurityUtil()
    {
    }


    public static final String CHARSET = "UTF-8";


    /**
     * 基于MD5算法的单向加密
     *
     * @param strSrc 明文
     * @return 返回密文
     */
    public static final String encryptMd5(String strSrc)
    {
        String outString = null;
        try
        {
            outString = encryptBASE64(MDCoder.encodeMD5(strSrc.getBytes(CHARSET)));
        } catch (Exception e)
        {
            throw new RuntimeException("加密错误，错误信息：", e);
        }
        return outString;
    }

    /**
     * BASE64编码
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static final String encryptBASE64(byte[] key)
    {
        try
        {
            return new BASE64Encoder().encode(key);
        } catch (Exception e)
        {
            throw new RuntimeException("加密错误，错误信息：", e);
        }
    }
    
    public static void main(String args[])
    {
        System.out.println(encryptMd5("password"));
    }

}
